#include"UnitSelectionWidget.h" 

#include"UnitFM.h"
#include"UnitHM.h"
#include"UnitLM.h"
#include"UnitLR.h"
#include"UnitSR.h"

UnitSelectionWidget::UnitSelectionWidget(QWidget *parent):QWidget(parent){
	m_layout = new QHBoxLayout(this);
	setLayout(m_layout);
	
	m_ageSelection = new QComboBox(this);
	m_layout->addWidget(m_ageSelection);
	QStringList ages;
	ages.push_back("Bronze Age");
	ages.push_back("Iron Age");
	ages.push_back("Early Middle Ages");
	ages.push_back("High Middle Ages");
	ages.push_back("Late Middle Ages");
	ages.push_back("Colonial Age");
	ages.push_back("Industrial Age");
	m_ageSelection->addItems(ages);
	connect(m_ageSelection, SIGNAL(currentIndexChanged(int)), this, SLOT(slotAgeIndexChanged(int)));
	
	m_unitSelection = new QComboBox(this);
	m_layout->addWidget(m_unitSelection);
	m_unitSelection->addItems(unitsForAge(AGE_BRZ));
	
	m_deleteButton = new QPushButton(QIcon::fromTheme("edit-delete"), "", this);
	m_layout->addWidget(m_deleteButton);
	connect(m_deleteButton, SIGNAL(clicked()), this, SLOT(slotDeleteThis()));
}

UnitSelectionWidget::~UnitSelectionWidget(){
	
}

void UnitSelectionWidget::slotDeleteThis(){
	emit deleteThis();
}

void UnitSelectionWidget::slotAgeIndexChanged(int age){
	UnitAge uAge = (UnitAge)age;
	m_unitSelection->clear();
	m_unitSelection->addItems(unitsForAge(uAge));
}

QStringList UnitSelectionWidget::unitsForAge(UnitAge age){
	QStringList ret;
	switch(age){
		case AGE_BRZ:
			ret.push_back("Spearfighter");
			ret.push_back("Horseman");
			ret.push_back("Warrior");
			ret.push_back("Slinger");
			ret.push_back("Stone Thrower");
			break;
		case AGE_IRN:
			ret.push_back("Soldier");
			ret.push_back("Mounted Warrior");
			ret.push_back("Legionnaire");
			ret.push_back("Archer");
			ret.push_back("Ballista");
			break;
		case AGE_EMA:
			ret.push_back("Mercenary");
			ret.push_back("Heavy Cavalry");
			ret.push_back("Armored Infantry");
			ret.push_back("Mounted Archer");
			ret.push_back("Catapult");
			break;
		case AGE_HMA:
			ret.push_back("Berserker");
			ret.push_back("Knight");
			ret.push_back("Heavy Infantry");
			ret.push_back("Crossbowman");
			ret.push_back("Trebuchet");
			break;
		case AGE_LMA:
			ret.push_back("Great Sword Warrior");
			ret.push_back("Heavy Knight");
			ret.push_back("Imperial Guard");
			ret.push_back("Longbow Archer");
			ret.push_back("Cannon");
			break;
		case AGE_CA:
			ret.push_back("Ranger");
			ret.push_back("Dragoon");
			ret.push_back("Grenadier");
			ret.push_back("Musketeer");
			ret.push_back("Field Gun");
			break;
		case AGE_IA:
			ret.push_back("Jaeger Infantry");
			ret.push_back("Lancer");
			ret.push_back("Howitzer");
			ret.push_back("Rifleman");
			ret.push_back("Breech Loader");
			break;
	}
	return ret;
}

Unit *UnitSelectionWidget::getUnit(Map *map, Team team)const{
	UnitType type = (UnitType) m_unitSelection->currentIndex();
	UnitAge age = (UnitAge) m_ageSelection->currentIndex();
	Unit *ret = 0;
	switch(type){
		case TYPE_LM:
			ret = new UnitLM(map, age, team);
			break;
		case TYPE_FM:
			ret = new UnitFM(map, age, team);
			break;
		case TYPE_HM:
			ret = new UnitHM(map, age, team);
			break;
		case TYPE_SR:
			ret = new UnitSR(map, age, team);
			break;
		case TYPE_LR:
			ret = new UnitLR(map, age, team);
			break;
	}
	return ret;
}

UnitAge UnitSelectionWidget::age()const{
	return (UnitAge) m_ageSelection->currentIndex();
}

UnitType UnitSelectionWidget::type()const{
	return (UnitType) m_unitSelection->currentIndex();
}

void UnitSelectionWidget::setAge(UnitAge age){
	m_ageSelection->setCurrentIndex(age);
	m_unitSelection->clear();
	m_unitSelection->addItems(unitsForAge(age));
}

void UnitSelectionWidget::setType(UnitType type){
	m_unitSelection->setCurrentIndex(type);
}